package cnsatmsmart.automation.mmi.controller.event;

import cnsatmsmart.automation.mmi.helper.utilities.GlobalEnvVars;
import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.MapBean;
import com.bbn.openmap.event.NavMouseMode;
import com.bbn.openmap.proj.Proj;
import com.bbn.openmap.proj.ProjMath;
import com.bbn.openmap.proj.Projection;
import com.bbn.openmap.util.Debug;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;

/**
 *
 * @author SIK - PTIK, BPPT
 */
public class NavMouseMode3 extends NavMouseMode {

    /* atribut yang dimiliki oleh kelas NavMouseMode3 */
        private JComboBox cbzoom;       //untuk mengatur combo box yang ada pada tampilan

    /**
     * Constructor - user defined constructor, no default constructor
     * @param cb combo box untuk zoom pada jendela utama
     */
        public NavMouseMode3(JComboBox cb)
        {
            super(true);
            this.cbzoom = cb;
        }


    /* method - method lain yang dimiliki oleh kelas NavMouseMode3 */
        @Override
        public void mouseReleased(MouseEvent e)
        {
            if (Debug.debugging("mousemode")) {
                Debug.output(getID() + "|NavMouseMode3.mouseReleased()");
            }

            Object obj = e.getSource();

            if (!this.mouseSupport.fireMapMouseReleased(e))
            {
                if ((!(obj instanceof MapBean)) || (!this.autoZoom) || (this.point1 == null) || (this.point2 == null))
                {
                    return;
                }
                MapBean map = (MapBean)obj;
                Projection projection = map.getProjection();
                Proj p = (Proj)projection;

                synchronized (this) {
                    this.point2 = getRatioPoint((MapBean)e.getSource(), this.point1, e.getPoint());

                    int dx = Math.abs(this.point2.x - this.point1.x);
                    int dy = Math.abs(this.point2.y - this.point1.y);

                    if ((dx < 5) || (dy < 5))
                    {
                        paintRectangle(map, this.point1, this.point2);

                        if ((dx < 5) && (dy < 5)) {
                            LatLonPoint llp = projection.inverse(e.getPoint());

                            boolean shift = e.isShiftDown();
                            boolean control = e.isControlDown();

                            if (control) {
                                if (shift)
                                    p.setScale(p.getScale() * 2.0F);
                                else {
                                    p.setScale(p.getScale() / 2.0F);
                                }
                            }

                            this.point1 = null;
                            this.point2 = null;

                            p.setCenter(llp);
                            map.setProjection(p);
                        }
                        return;
                    }

                    dx = Math.abs(this.point2.x - this.point1.x);
                    dy = Math.abs(this.point2.y - this.point1.y);

                    Point cornerPoint1 = new Point(this.point2.x < this.point1.x ? this.point2.x : this.point1.x, this.point2.y < this.point1.y ? this.point2.y : this.point1.y);

                    Point cornerPoint2 = new Point(cornerPoint1.x + 2 * dx, cornerPoint1.y + 2 * dy);

                    float newScale = ProjMath.getScale(cornerPoint1, cornerPoint2, projection);

                    LatLonPoint center = projection.inverse(this.point1.x, this.point1.y);

                    p.setScale(newScale);

                    float temp = ((GlobalEnvVars.maxScale-newScale)/4975000*100);
                    if (temp >= 1f)
                        cbzoom.setSelectedItem((int)temp+"%");
                    else
                    {
                        String str = String.format("%.2f", temp);
                        cbzoom.setSelectedItem(str+"%");
                    }
                    p.setCenter(center);

                    this.point1 = null;
                    this.point2 = null;

                    map.setProjection(p);
                }
            }
        }

        /**
         * menggambarkan kotak pada layar
         * @param g grafik untuk menggambarkan garis untuk kotak
         * @param pt1 titik pertama ketika mouse ditekan
         * @param pt2 titik kedua ketika mouse dilepaskan
         */
        @Override
        protected void paintRectangle(Graphics g, Point pt1, Point pt2)
        {
            g.setXORMode(Color.lightGray);
            g.setColor(Color.darkGray);

            if ((pt1 != null) && (pt2 != null))
            {
                int width = Math.abs(pt2.x - pt1.x);
                int height = Math.abs(pt2.y - pt1.y);

                if (width == 0)
                    width++;
                if (height == 0) {
                    height++;
                }
                g.drawRect(pt1.x - width, pt1.y - height, width * 2, height * 2);
                g.drawRect(pt1.x - 1, pt1.y - 1, 3, 3);
            }
        }
}